#include "BombEffect.h"
#include "../common.h"

namespace fireplane{

	BombEffect::BombEffect( EffectType type ):
		Effect(type),_bomb0(NULL),_bomb1(NULL)
	{}

	BombEffect::~BombEffect()
	{}

	void BombEffect::initBomb0OrBomb1(CCPoint worldpos){
		char buffer[0xff];
		sprintf(buffer,"effect/bomb%d/bomb0.png",(int)_type);

		_bomb0 = CCSprite::create(buffer);
		addChild(_bomb0);
		_bomb0->runAction(CCScaleTo::create(0.2f,6.0f));
		_bomb0->runAction(
			CCSequence::create(
			CCFadeOut::create(0.2f),
			CCCallFunc::create(this,callfunc_selector(BombEffect::destroyThis)),
			NULL));
		_bomb0->setRotation((float)((int)rand() % 360));
		_bomb0->runAction(CCRotateBy::create(0.2f,45));

		sprintf(buffer,"effect/bomb%d/bomb1.png",(int)_type);
		_bomb1 = CCSprite::create(buffer);
		addChild(_bomb1);
		_bomb1->runAction(CCScaleTo::create(0.2f,10.0f));
		_bomb1->runAction(CCFadeOut::create(0.2f));

		worldpos = Effect::getAllEffectParent()->convertToNodeSpace(worldpos);
		this->setPosition(worldpos);
	}

	void BombEffect::init(CreateCmd* cmd)
	{
		switch(cmd->_id){
		case Effect::ET_bomb0:
		case Effect::ET_bomb1:
			{
				CreateBomb0OrBomb1* tcmd = (CreateBomb0OrBomb1*)cmd;
				initBomb0OrBomb1(tcmd->worldPos);
			}
			break;

		case Effect::ET_bomb2:
			{
				CreateBomb2* tcmd = (CreateBomb2*)cmd;
				initBomb2(tcmd->worldPos,tcmd->path,tcmd->animationCount,tcmd->rect);
			}
			break;
		}
	}

	void BombEffect::initBomb2( CCPoint pos,string path,int animationCount,CCRect rect)
	{
		_bomb0 = NULL;
		_bomb1 = NULL;

		char buffer[0xff];
		sprintf(buffer,"%s1.png",path.c_str());
		_bomb0 = CCSprite::create(buffer);
		addChild(_bomb0);

		pos = Effect::getAllEffectParent()->convertToNodeSpace(pos);
		this->setPosition(pos);

		CCActionInterval* action = Common::createFrameAnimation(path,animationCount,rect);
		CCCallFunc* call = CCCallFunc::create(this,callfunc_selector(BombEffect::destroyThis));
		action = CCSequence::create(action,call,NULL);
		_bomb0->runAction(action);
		_bomb0->runAction(CCScaleTo::create(0.5f,10.0f,10.0f));

		sprintf(buffer,"%sb1.png",path.c_str());
		_bomb1 = CCSprite::create(buffer);
		addChild(_bomb1);
		_bomb1->runAction(CCScaleTo::create(0.5f,10.0f,10.0f));
	}

	void BombEffect::destroyThis()
	{
		this->removeFromParentAndCleanup(true);
	}

}